<?php
namespace App\Http\Questionnaire\Controllers;

use Illuminate\Http\Request;
use App\Http\Admin\Controllers\Controller as Controller;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\DB;


class  ActionController extends Controller
{
    public function __construct(Request $request){
        $token = getToken($request);
        $uid =Redis::get("code:".$token);
        $this->uid=$uid;
    }

    /**
     * 新增文章分类
     */
    function add_action(Request $request){
        $data = $request->only("title","description","faId","keywords","route");

        //title不存在 格式为空白字符 长度少于2大与20
        if(!array_key_exists('title',$data)||!preg_match('/^[\S\s]{2,20}$/u',$data['title'])) return ThrowApiExc('32001');
        //description不存在 格式为空白字符 长度大于100
        if(!array_key_exists('description',$data)||!preg_match('/^[\S]{0,100}$/u',$data['description'])) return ThrowApiExc('32003');

        $data['type']=1;
        $news['createdTime']=date("Y-m-d H:i:s",time());
        $news['updateTime']=date("Y-m-d H:i:s",time());
        $data['uid']=$this->uid;
        $res = DB::table("action")->insertGetId($data);
        if($res){
            return [
                'status_code'      => 200,
                'message'     =>  '提交成功',
                'data' => $res
            ];
        }else{
            return [
                'status_code'      => 200,
                'message'     =>  '提交失败 联系后端开发',
                'data' => $res
            ];
        }


    }

    /**
     * 修改文章分类
     */
    function update_action(Request $request,$cid){

        $data = $request->only("title","description","faId","keywords","route","tit");

        //title不存在 格式为空白字符 长度少于2大与20
        if(array_key_exists('title',$data)&&!preg_match('/^[\S\s]{2,20}$/u',$data['title'])) return ThrowApiExc('32001');
        //description不存在 格式为空白字符 长度大于100
        if(array_key_exists('description',$data)&&!preg_match('/^[\S]{0,100}$/u',$data['description'])) return ThrowApiExc('32003');

        $data['type']=1;
        $news['updateTime']=date("Y-m-d H:i:s",time());
        $res = DB::table("action")->where("id",$cid)->update($data);

        if($res){
            return [
                'status_code'      => 200,
                'message'     =>  '提交成功',
                'data' => $res
            ];
        }else{
            return [
                'status_code'      => 200,
                'message'     =>  '提交失败 联系后端开发',
                'data' => $res
            ];
        }


    }

    public function delete_action($cid){
        $res = DB::table("action")->where("id",$cid)->update(["status"=>9]);
        if($res){
            return [
                'status_code'      => 200,
                'message'     =>  '提交成功',
                'data' => $res
            ];
        }else{
            return [
                'status_code'      => 200,
                'message'     =>  '提交失败 联系后端开发',
                'data' => $res
            ];
        }
    }

    public function select_action($type){
        $data = DB::table("action")->where([["type",$type],["status",1]])->get();

        return $this->select_action_tree($data);
    }

    public function select_action_tree($data,$faId=0){
        $new_data=array();
        foreach($data as $k=>$v){
            if($v['faId']==$faId){
                $linshi=[];
                $linshi=$v;
                $linshi['data']=$this->select_action_tree($data,$v['id']);
                $new_data[]=$linshi;
            }
        }
        return $new_data;
    }


    public function update_all_action(){
        $data=DB::table("c_case")->where("case_action","<",1772)->select("id","case_action","title")->get()->toArray();
        foreach($data as $k=>$v){
            $action=DB::table("c_case_action")->where("old_id",$v['case_action'])->first();
            DB::table("c_case")->where("id",$v['id'])->update(["case_action"=>$action['id']]);
        }
        return 123;
    }


}